<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-btn-container">
    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" id="add"> 添加配置项 </button>
</div>
<div class="layui-form layuimini-form">
    {volist name="list" id="vo"}
        {if $vo.type == 'input'}
            <div class="layui-form-item">
                <label class="layui-form-label">{$vo.setting.title}</label>
                <div class="layui-inline">
                    <input type="text" name="{$vo.fieldname}" value="{$configMap[$vo.fieldname]}" autocomplete="off" placeholder="{$vo.setting.tips}" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm" onclick="editItem({$vo.fid})">编辑</button>
                    <button class="layui-btn layui-btn-danger layui-btn-sm deleteItem" fid="{$vo.fid}">删除</button>
                </div>
            </div>
        {/if}
        {if $vo.type == 'radio'}
            <div class="layui-form-item">
                <label class="layui-form-label">{$vo.setting.title}</label>
                <div class="layui-inline">
                    {volist name="$vo.setting.options" id="op"}
                        <input type="radio" name="{$vo.fieldname}" value="{$op.value}" 
                            title="{$op.title}" {if $configMap[$vo.fieldname] == $op.value}checked{/if}>
                    {/volist}
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm" onclick="editItem({$vo.fid})">编辑</button>
                    <button class="layui-btn layui-btn-danger layui-btn-sm deleteItem" fid="{$vo.fid}">删除</button>
                </div>
            </div>
        {/if}

        {if $vo.type == 'textarea'}
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">{$vo.setting.title}</label>
                <div class="layui-inline">
                    <textarea placeholder="{$vo.setting.tips}" class="layui-textarea" name="{$vo.fieldname}">{$configMap[$vo.fieldname]}</textarea>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm" onclick="editItem({$vo.fid})">编辑</button>
                    <button class="layui-btn layui-btn-danger layui-btn-sm deleteItem" fid="{$vo.fid}">删除</button>
                </div>
            </div>
        {/if}
        {if $vo.type == 'select'}
            <div class="layui-form-item">
                <label class="layui-form-label">{$vo.setting.title}</label>
                <div class="layui-inline">
                    <select name="{$vo.fieldname}" lay-filter="aihao">
                        {volist name="$vo.setting.options" id="op"}
                            <option value="{$op.value}" {if $configMap[$vo.fieldname] == $op.value}selected{/if}>{$op.title}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm" onclick="editItem({$vo.fid})">编辑</button>
                    <button class="layui-btn layui-btn-danger layui-btn-sm deleteItem" fid="{$vo.fid}">删除</button>
                </div>
            </div>
        {/if}
    {/volist}
    {if $list}
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    {/if}
</div>
<script src="/static/admin/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    function editItem(fid){
        var content = "{:url('Config/editExtend')}"+"?fid="+fid;
        var index = layer.open({
            title: '编辑扩展配置',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['80%','80%'],
            content: content,
            end: function(){
                //执行搜索重载
                location.reload();
            }
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
    }

    
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        
        //删除扩展配置
        $(".deleteItem").click(function(){
            var fid = $(this).attr('fid');
            $.ajax({
                url: "{:url('Config/deleteExtend')}",
                type: 'post',
                dataType: 'json',
                data:{fid:fid},
                success: function (data) {
                    layer.msg(data.msg);
                    //删除成功
                    if(data['status']){
                        setTimeout(function(){
                            location.reload();
                        }, 2000)
                    }
                },
                error: function (xhr, textstatus, thrown) {
                    return layer.msg('Status:' + xhr.status + '，' + xhr.statusText + '，请稍后再试！');
                }
            });
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: "{:url('Config/extend')}",
                type: 'post',
                dataType: 'json',
                data:data.field,
                success: function (data) {
                    layer.msg(data.msg);
                    //编辑成功
                    if(data['status']){
                        setTimeout(function(){
                           location.reload();
                        }, 2000)
                    }
                },
                error: function (xhr, textstatus, thrown) {
                    return layer.msg('Status:' + xhr.status + '，' + xhr.statusText + '，请稍后再试！');
                }
            });

            return false;
        });
        
        //添加扩展配置
        $("#add").click(function(){
            var content = "{:url('Config/addExtend')}";
            var index = layer.open({
                title: '添加扩展配置',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['80%','80%'],
                content: content,
                end: function(){
                    //执行搜索重载
                    location.reload();
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        })

    });
</script>
</body>
</html>